#include "view_time_15.h"
#include "view_time_15.inc"
#include "view_time_14.h"
#include "view_time_3.h"
#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "view_times.h"
#include "math.h"
#include "weather_ctrl.h"
#include "health_storage.h"
#include "app_trace.h"
#include "platform_assert.h"
#define LOG(fmt, ...) //app_trace_log(fmt "\r\n", ##__VA_ARGS__)
#ifdef TIME_15_SKIP
#else

static void show_weather(void)
{
    int minimum_temperature = disp_ctrl.com_data.i_temp[19];
    int maximum_temperature = disp_ctrl.com_data.i_temp[20];

    int const font_height = 13;
    int const font_width = 8;
    int const font_interval = 1;
    int const font_unit_interval = 1;
    int const unit_width = 4;
    int const unit_slash_interval = -1;
    int const slash_width = 6;
    int const slash_number_interval = 3;
    // int x = 72 - 26;
    int x = 184;
    int y = 68;
#if 0
#warning debug
    disp_ctrl.com_data.i_temp[22] = 1;
    minimum_temperature = 12;
    maximum_temperature = 3;
#endif
    if (disp_ctrl.com_data.i_temp[22] == 0)
    {
        x = 197;
        disp_draw_rect_color(x, y + font_height / 2 - 1, x + font_width - 1, y + font_height / 2, 0xce59);
        x += font_width + 2;
        disp_draw_rect_color(x, y + font_height / 2 - 1, x + font_width - 1, y + font_height / 2, 0xce59);
        disp_draw_user_image(191, 25, img_time_3_weather_unknown);
        return;
    }
    int total_width = 0;
    int const min_digits = get_num_digit(minimum_temperature);
    int const max_digits = get_num_digit(maximum_temperature);
    total_width += min_digits * font_width + (min_digits - 1) * font_interval;
    total_width += max_digits * font_width + (max_digits - 1) * font_interval;
    total_width += font_unit_interval + unit_width;
    total_width += slash_width + slash_number_interval;
    total_width += font_unit_interval + unit_width;
    int end_x = 230;
    x = end_x - total_width;

    if (minimum_temperature < 0)
    {
        disp_draw_rect_color(x, y + font_height / 2 - 1, x + font_width - 1, y + font_height / 2, 0xce59);
        x += font_width + font_interval;
    }
    x = disp_draw_user_image_num(x, y, minimum_temperature, font_width + font_interval, num_8x13_TAB);
    x -= font_interval;
    x += font_unit_interval;
    // draw unit
    disp_draw_user_image(x, y, degrees);
    x += unit_width + unit_slash_interval;
    // draw slash
    disp_draw_user_image_filter_color(x, y, slash);
    x += slash_width + slash_number_interval;
    if (maximum_temperature < 0)
    {
        disp_draw_rect_color(x, y + font_height / 2 - 1, x + font_width - 1, y + font_height / 2, 0xce59);
        x += font_width + font_interval;
    }
    x = disp_draw_user_image_num(x, y, maximum_temperature, font_width + font_interval, num_8x13_TAB);
    x -= font_interval;
    x += font_unit_interval;
    // draw unit
    disp_draw_user_image(x, y, degrees);

    uint8_t const *weather_icon = 0;
    switch (disp_ctrl.com_data.i_temp[18])
    {
    case WEATHER_TYPE_SUNSHINE: //晴天
        weather_icon = img_time_3_weather_sun;
        break;
    case WEATHER_TYPE_CLOUDY: //多云
        weather_icon = img_time_3_weather_cloudy_day;
        break;
    case WEATHER_TYPE_RAINY: //雨天
        weather_icon = img_time_3_weather_rain;
        break;
    case WEATHER_TYPE_SNOW: //雪
        weather_icon = img_time_3_weather_snowing;
        break;
    case WEATHER_TYPE_HAZE: //雾霾
        weather_icon = img_time_3_weather_foggy_day;
        break;
    case WEATHER_TYPE_PARTLY_CLOUDY:
        weather_icon = img_time_3_weather_partly_cloudy;
        break;
    case WEATHER_TYPE_UNKNOW:
        weather_icon = img_time_3_weather_unknown;
        break;
    }
    if (weather_icon != 0)
    {
        disp_draw_user_image(194, 25, weather_icon);
    }
}
/**
 * @brief Get the heart rate object
 *
 * @param buffer
 * @param count
 * @param start_index
 * @param end_index
 * @return int max_heart_rate 0 means invalid
 */
static int get_heart_rate(uint8_t *buffer, uint32_t count, int start_index, int end_index)
{
    uint32_t zero_utc = rtc_get_zero_utc();
    uint32_t first_second = zero_utc + 8 * 60 * 60;
    health_data_heart_t heart_rate = {0};
    uint32_t band_second = 0;
    // #warning debug
    //     first_second = 1591055942 + 24 * 60 * 60;
    unix_ts_to_band_ts(first_second, &band_second);
    uint8_t max_heart_rate = 0;
    int result = health_storage_get_heart_data(band_second, &heart_rate);
    // LOG("get_heart_rate %d %d %d", result, first_second, band_second);
    if (result)
    {
        for (int index = start_index; index <= end_index; index++)
        {
            if (heart_rate.data[index] >= 40 && heart_rate.data[index] <= 220)
            {
                if (heart_rate.data[index] > max_heart_rate)
                {
                    max_heart_rate = heart_rate.data[index];
                }
                buffer[index - start_index] = heart_rate.data[index];
            }
        }
    }
    return max_heart_rate;
}
#define SHOW_TOP_Y 99
#define SHOW_BOTTOM_Y 162
#define SHOW_HEIGHT (SHOW_BOTTOM_Y - SHOW_TOP_Y)

#define SHOW_LEFT_X 11
#define SHOW_RIGHT_X 206

#define SHOW_LEFT_TAG_X 20
#define SHOW_RIGHT_TAG_X 214

#define SHOW_LEFT_TAG_INDEX (9 * 12)
#define SHOW_RIGHT_TAG_INDEX (21 * 12)

#define INDEX_EACH_PIXEL (((float)(SHOW_RIGHT_TAG_INDEX - SHOW_LEFT_TAG_INDEX)) / (SHOW_RIGHT_TAG_X - SHOW_LEFT_TAG_X))
#define SHOW_LEFT_INDEX (SHOW_LEFT_TAG_INDEX - (SHOW_LEFT_TAG_X - SHOW_LEFT_X) * INDEX_EACH_PIXEL)

#define GET_INDEX_BY_X(x) (SHOW_LEFT_INDEX + ((x)-SHOW_LEFT_X) * INDEX_EACH_PIXEL)
#define GET_OFFSET_BY_X(x) (GET_INDEX_BY_X(x) - SHOW_LEFT_INDEX)

#define GET_SHOW_Y_BY_HEART_RATE(hr, top) (SHOW_BOTTOM_Y - SHOW_HEIGHT * (hr) / (top))

#define TOP_HEART_RATE_OFFSET 46
#define BUFFER_START_OFFSET (TOP_HEART_RATE_OFFSET + 1)
#define BUFFER_SIZE (DISP_COM_BUFF_SIZE - BUFFER_START_OFFSET * 4)
PLATFORM_STATIC_ASSERT(SHOW_RIGHT_X + 1 - SHOW_LEFT_X <= BUFFER_SIZE, "size small");

static void show_heart_rate(void)
{
    uint8_t *top_heart_rate = (uint8_t *)&disp_ctrl.com_data.i_temp[TOP_HEART_RATE_OFFSET];
    uint8_t *buffer_start = (uint8_t *)&disp_ctrl.com_data.i_temp[BUFFER_START_OFFSET];
    uint32_t buffer_size = ((uint8_t *)(disp_ctrl.com_data.i_temp)) + sizeof(disp_ctrl.com_data.i_temp) - buffer_start;
    // 41
    if (disp_ctrl.loop_cnt == 0)
    {
        // LOG("buffer_size %u", buffer_size);
        int start_index = GET_INDEX_BY_X(SHOW_LEFT_X);
        int end_index = GET_INDEX_BY_X(SHOW_RIGHT_X);

        uint8_t heart_rate_data[HEART_DATA_NUM_PER_DAY] = {0};
        *top_heart_rate = get_heart_rate(heart_rate_data, sizeof(heart_rate_data), start_index, end_index);
        *top_heart_rate += (*top_heart_rate) / 10;

        memset(buffer_start, 0, buffer_size);
        uint8_t heart_rate_value = 0;
        int offset = 0;
        for (int i = SHOW_LEFT_X; i <= SHOW_RIGHT_X; i++)
        {
            offset = GET_OFFSET_BY_X(i);
            heart_rate_value = heart_rate_data[offset];
#if 0
#warning debug
            heart_rate_value = i % (*top_heart_rate);
#endif
            if (heart_rate_value == 0)
            {
                continue;
            }
            buffer_start[i - SHOW_LEFT_X] = GET_SHOW_Y_BY_HEART_RATE(heart_rate_value, *top_heart_rate);
            // LOG("i:%d index:%d hr:%d y:%d", i, offset, heart_rate_value, buffer_start[i - SHOW_LEFT_X]);
        }
    }
    disp_draw_heart_rate_rect_filter_color(SHOW_LEFT_X, SHOW_RIGHT_X, buffer_start, 0xf9ac, 0x4823, SHOW_BOTTOM_Y);
    if (*top_heart_rate != 0)
    {
        int tag_0 = (*top_heart_rate) / 3;
        int tag_1 = (*top_heart_rate) * 2 / 3;
        int tag_2 = (*top_heart_rate);
        int x = 209;
        int y = 92;
        disp_draw_user_image_num(x, y, tag_2, 6, num_6x10_TAB);
        y = 114;
        disp_draw_user_image_num(x, y, tag_1, 6, num_6x10_TAB);
        y = 136;
        disp_draw_user_image_num(x, y, tag_0, 6, num_6x10_TAB);
    }
    // LOG("tag %d %d %d", tag_0, tag_1, tag_2);
}

void view_time_15_display(void)
{
    int month = 0;
    int day = 0;
    int hour = 0;
    int second = 0;
    int calories = disp_ctrl.com_data.i_temp[16];
    int steps = disp_ctrl.com_data.i_temp[11];
    int heart_rate = disp_ctrl.com_data.i_temp[17];
    month = disp_ctrl.com_data.i_temp[1];
    day = disp_ctrl.com_data.i_temp[2];
    hour = disp_ctrl.com_data.i_temp[3];
    second = disp_ctrl.com_data.i_temp[4];
    int hour_12_type = disp_ctrl.com_data.i_temp[9];
#if 0
#warning debug
    steps = 13132;
    heart_rate = 12323;
#endif
    int x = 0;
    int y = 0;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_hm_4);
    show_time_0_battery(disp_ctrl.com_data.i_temp[7], disp_ctrl.com_data.i_temp[8]);
    show_time_0_ble();
    show_weather();
    disp_draw_user_image(66, 69, img_time_0_week_TAB[disp_ctrl.com_data.i_temp[5] - 1]);
    disp_draw_user_image_num_with_digits(9, 69, month, 2, 10, num_8x13_TAB);
    disp_draw_user_image_num_with_digits(34, 69, day, 2, 10, num_8x13_TAB);
    disp_draw_user_image_num_with_digits(8, 22, hour, 2, 24, num_19x37_TAB);
    disp_draw_user_image_num_with_digits(66, 22, second, 2, 24, num_19x37_TAB);

    y = 208;
    x = 33;
    int valid_heart_rate = 0;
    int const max_heart_rate = 220 - system_config_get_age();
    int const min_heart_rate = 40;
    if (heart_rate < 40 || heart_rate > 220)
    {
        int x1 = x - 7;
        disp_draw_rect_color(x1, y + 10 / 2, x1 + 7 - 1, y + 10 / 2 + 1, RGB565_COLOR_WHITE);
        x1 += 8;
        disp_draw_rect_color(x1, y + 10 / 2, x1 + 7 - 1, y + 10 / 2 + 1, RGB565_COLOR_WHITE);
        valid_heart_rate = 0;
    }
    else
    {
        valid_heart_rate = heart_rate - min_heart_rate;
        disp_draw_user_image_num_mid_filter_color(x, y, heart_rate, 7, num_7x10_TAB);
    }
    x = 205;
    disp_draw_user_image_num_mid_filter_color(x, y, steps, 7, num_7x10_TAB);
    x = 97 + 23;
    disp_draw_user_image_num_mid_filter_color(x, y, calories, 7, num_7x10_TAB);
    if (hour_12_type == 1)
    {
        disp_draw_user_image_filter_color(115, 48, icon_am);
    }
    else if (hour_12_type == 2)
    {
        disp_draw_user_image_filter_color(115, 43, icon_pm);
    }
    {
        user_cycle_percent_param_t const param = {
            .base = circle_base,
            .transparent_color = RGB565_COLOR_BLACK,
            .full_color = 0xe18b,
            .empty_color = 0x30e3,
            .x = 10,
            .y = 190,
            .target_num = max_heart_rate - min_heart_rate, // 1000
            .presently_num = valid_heart_rate,             // disp_ctrl.com_data.i_temp[12], // 2600
            .temp_0 = &disp_ctrl.com_data.i_temp[31],
            .temp_1 = &disp_ctrl.com_data.i_temp[32],
            .temp_2 = &disp_ctrl.com_data.f_temp[33],
            .temp_3 = &disp_ctrl.com_data.i_temp[34],
            .temp_4 = &disp_ctrl.com_data.i_temp[35],
            .line_width = 3,
        };
        disp_draw_user_image_cycle_percent(&param);
    }
    {
        user_cycle_percent_param_t const param = {
            .base = circle_base,
            .transparent_color = RGB565_COLOR_BLACK,
            .full_color = 0xbde,
            .empty_color = 0x18e8,
            .x = 183,
            .y = 190,
            .target_num = system_config_get_target_step(), // 1000
            .presently_num = steps,                        // disp_ctrl.com_data.i_temp[12], // 2600
            .temp_0 = &disp_ctrl.com_data.i_temp[36],
            .temp_1 = &disp_ctrl.com_data.i_temp[37],
            .temp_2 = &disp_ctrl.com_data.f_temp[38],
            .temp_3 = &disp_ctrl.com_data.i_temp[39],
            .temp_4 = &disp_ctrl.com_data.i_temp[40],
            .line_width = 3,
        };
        disp_draw_user_image_cycle_percent(&param);
    }

    {
        user_cycle_percent_param_t const param = {
            .base = circle_base,
            .transparent_color = RGB565_COLOR_BLACK,
            .full_color = 0xe301,
            .empty_color = 0x30e3,
            .x = 97,
            .y = 190,
            .target_num = system_config_get_target_calorie(), // 1000
            .presently_num = calories,                        // disp_ctrl.com_data.i_temp[12], // 2600
            .temp_0 = &disp_ctrl.com_data.i_temp[41],
            .temp_1 = &disp_ctrl.com_data.i_temp[42],
            .temp_2 = &disp_ctrl.com_data.f_temp[43],
            .temp_3 = &disp_ctrl.com_data.i_temp[44],
            .temp_4 = &disp_ctrl.com_data.i_temp[45],
            .line_width = 3,
        };
        disp_draw_user_image_cycle_percent(&param);
    }
    show_heart_rate();
}
#endif
